package com.ywqf_api.controller;

import com.ywqf_api.common.enums.FilePaths;
import com.ywqf_api.dto.exc.ReturnExcution;
import com.ywqf_api.entity.ParkingData;
import com.ywqf_api.enums.ReturnEnum;
import com.ywqf_api.service.BoundHouseService;
import com.ywqf_api.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Administrator on 2018/4/8 0008.
 */
@Api(description="boundHouse接口",produces="application/json")
@Controller
@RequestMapping("boundHouse")
public class BoundHouseController {
    @Autowired
    private BoundHouseService boundHouseService;

    /**
     * 绑定房屋 查是否存在该房屋
     * @param communityId
     * @param houseNum
     * @param ownerName
     * @param ownerTel
     * @return
     */
    @ApiOperation(value = "绑定房屋 查是否存在该房屋", httpMethod = "POST", notes = "绑定房屋 查是否存在该房屋")
    @RequestMapping(value = "findIsNull",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findIsNull(Integer communityId,String houseNum,String ownerName,String ownerTel){
        int house = boundHouseService.findIsNull(communityId,houseNum,ownerName,ownerTel);
        if(house == 0){
            return new ReturnExcution(ReturnEnum.NOT_FIND_HOUSE,null);
        }
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,house);
    }


    /**
     * 绑定房屋
     * @param houseId
     * @param customerId
     * @return
     */
    @ApiOperation(value = "绑定房屋", httpMethod = "POST", notes = "绑定房屋")
    @RequestMapping(value = "updateCustomerHouseId",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution updateCustomerHouseId(Integer houseId,Integer customerId){
        int house = boundHouseService.updateCustomerHouseId(houseId,customerId);
        if(house == 0){
            return new ReturnExcution(ReturnEnum.UPDATE_FILE,null);
        }
        return new ReturnExcution(ReturnEnum.UPDATE_SUCCESS,house);
    }

    /**
     * 我的资料  图片处理
     * @param file
     * @return
     */
    @ApiOperation(value = "图片处理", httpMethod = "POST", notes = "图片处理")
    @RequestMapping(value = "file",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution file(MultipartFile file){
        String url = UploadUtil.saveFile(file, FilePaths.USER);
        if(url == null || url.equals(null)){
            return new ReturnExcution(ReturnEnum.SAVE_FILE_FAIL,null);
        }
        url = "developer/"+url;
        return new ReturnExcution(ReturnEnum.ADD_SUCCESS,url);
    }


    /**
     * 我的资料      修改
     * @param customerId
     * @return
     */
    @ApiOperation(value = "我的资料", httpMethod = "POST", notes = "我的资料")
    @RequestMapping(value = "updateCustomer",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution updateCustomer(Integer customerId,String url,String name,Integer gender){
        int house = boundHouseService.updateCustomer(customerId,url,name,gender);
        if(house == 0){
            return new ReturnExcution(ReturnEnum.UPDATE_FILE,null);
        }
        return new ReturnExcution(ReturnEnum.UPDATE_SUCCESS,house);
    }

    /**
     * 查房屋对应的车位
     * @param houseId
     * @return
     */
    @ApiOperation(value = "查房屋对应的车位", httpMethod = "POST", notes = "查房屋对应的车位")
    @RequestMapping(value = "findHouseCars",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findHouseCars(Integer houseId){
        List<ParkingData> list = boundHouseService.findHouseCars(houseId);
        if(list == null){
            return new ReturnExcution(ReturnEnum.FIND_CARS_NULL,null);
        }
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,list);
    }

}
